package com.master.config;

import com.master.domain.MiaoshaUser;
import org.springframework.stereotype.Component;

/**
将ThreadLocal封装在HostHolder中,创建拦截器首先拦截用户请求
 如果能从token中获取MiaoshaUser信息,则将用户信息存储在ThreadLocal中
 以供后端的其他业务模块访问
 */
@Component
public class HostHolder {

    private static ThreadLocal<MiaoshaUser> users = new ThreadLocal<>();

    public MiaoshaUser getUser(){
        return users.get();
    }

    public void setUser(MiaoshaUser user){
        users.set(user);
    }

    public void clear(){
        users.remove();
    }

}
